﻿// ActionScript file
/**
 * NestedMenu的主按钮类，一般单独初始化，跟随NestedMenu创建
 * 
 * mainButton&&subButton都是扩展自ListItemSkin
 */
package org.nickro.controls{
	import caurina.transitions.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.nickro.events.ItemClickEvent;
	import org.nickro.geom.EdgeMetrics;
	
	public class MainButton extends BaseControls{
		public static const MAIN_OUT:String="MainOut";
		public static const MAIN_OVER:String="MainOver";
		public static const MAIN_CLICK:String="MainClick";
		
		private var _mainButton:Class;
		private var _subButton:Class;
		private var _labelPlace:*;
		private var _subEdge:EdgeMetrics=new EdgeMetrics();
		private var _subDirection:String="horizontal";
		private var _subHorizontalGap:int=2;
		private var _subVerticalGap:int=2;
		private var _subStartIndex:int=-1;
		
		private var mainBtn:MovieClip;
		private var subList:List;
		private var itemNum:int;
		public function MainButton(dataProvider:Object=null,mainButton:Class=null,subButton:Class=null,labelPlace:String=null)
		{
			_mainButton=mainButton;
			_subButton=subButton;
			_labelPlace=labelPlace;
			super(dataProvider);
			//trace(data);
		}
		override protected function createChildren():void{
		 	createMainButton();
		}
		
		private function createMainButton():void{
			
			mainBtn=new _mainButton();
			mainBtn.dataProvider=data;
			addChild(mainBtn);
			mainBtn.addEventListener(MouseEvent.MOUSE_DOWN,onMainDown);
			mainBtn.addEventListener(MouseEvent.MOUSE_OVER,onMainOver);
			mainBtn.addEventListener(MouseEvent.MOUSE_OUT,onMainOut);
		}
		private function createSubList():void{
			//动态节�?_labelPlace);
			var subData:Object=XML(data)..subarea;
			
			//trace(subData);
			/* if(subData is null){
				trace("ok")
				trace("SubList:XML解析失败,不存在的'tag'");
			}else{ */
				
				subList=new List(subData,_subButton);
				addChild(subList);
				subList.addEventListener(ItemClickEvent.ITEM_CLICK,onSubDown);
				subList.edge=_subEdge;
				subList.direction=_subDirection;
				subList.horizontalGap=_subHorizontalGap;
				subList.verticalGap=_subVerticalGap;
				subList.startIndex=_subStartIndex;  
				//trace(subList.y);
			//}
			
		}
		//整体布局
		override protected function updateDislayObject():void{
				//trace(this.width,this.height);
		}
		/**
		 * Event Hanlders
		 */
		private function onMainDown(e:MouseEvent):void{
			createSubList();
			var i:int=0;
			//点击后再次发出屏幕更�? 为了更新加入list后的width&height;
			addEventListener(Event.ENTER_FRAME,function onLoop(e:Event):void{
				i++;
				setChanged();
				if(i>1){
					removeEventListener(Event.ENTER_FRAME,onLoop);
				}
			});
			
			dispatchEvent(new Event(MainButton.MAIN_CLICK));
		}
		
		private function onMainOut(e:MouseEvent):void{
			dispatchEvent(new Event(MainButton.MAIN_OUT));
		}
		private function onMainOver(e:MouseEvent):void{
			dispatchEvent(new Event(MainButton.MAIN_OVER));
		}
		
		
		private function onSubDown(e:ItemClickEvent):void{
			//trace(e.index);
		}
		/**
		 * public setter/getter
		 */
		public function get mainSelected():Boolean{
			return mainBtn.selected;
		}
		public function set mainSelected(value:Boolean):void{
			mainBtn.selected=value;
		}
		
		public function set startIndex(value:int):void{
			_subStartIndex=value;
		}
		
		public function set subEdge(value:EdgeMetrics):void{
			_subEdge=value;
		}
		public function get subEdge():EdgeMetrics{
			return _subEdge;
		}
		
		public function set subDirection(value:String):void{
			_subDirection=value;
		}
		public function set subHorizontalGap(value:int):void{
			_subHorizontalGap= value;
		}
		public function set subVerticalGap(value:int):void{
			_subVerticalGap=value;
		}
		/** only read*/
		public function get mainWidth():Number{
			return mainBtn.width;
		}
		public function get mainHeight():Number{
			return mainBtn.height;
		}
	}
}